import { ui } from "./../ui/layaMaxUI";

import  CameraMoveScript  from "./common/CameraMoveScript";
import  FirstPlayerCtl  from "./models/FirstPlayerCtl";
import  EntityAnimCtl  from "./models/EntityAnimCtl";
import  EffectAnimCtl  from "./models/EffectAnimCtl";
import  FirstPlayerMoveCtl from "./models/FirstPlayerMoveCtl";

import  * as Def  from "./consts/Def";

/**
 * 本示例采用非脚本的方式实现，而使用继承页面基类，实现页面逻辑。在IDE里面设置场景的Runtime属性即可和场景进行关联
 * 相比脚本方式，继承式页面类，可以直接使用页面定义的属性（通过IDE内var属性定义），比如this.tipLbll，this.scoreLbl，具有代码提示效果
 * 建议：如果是页面级的逻辑，需要频繁访问页面内多个元素，使用继承式写法，如果是独立小模块，功能单一，建议用脚本方式实现，比如子弹脚本。
 */
export default class GameUI extends ui.d3.DiabloSceneUI {
    private _up = new Laya.Vector3(0,1,0);
    private _animator:Laya.Animator;
    private _startPos:Laya.Vector3;
    private _bornPos:Laya.Vector3;
    constructor() {
        super();

		this._startPos = new Laya.Vector3(-87, 5, -4);
        this._bornPos = new Laya.Vector3(-83, -2.5, -6);
        
        let cameraDela = new Laya.Vector3();
        Laya.Vector3.subtract(this._startPos, this._bornPos, cameraDela);
      
        var scene: Laya.Scene3D = Laya.stage.addChild(new Laya.Scene3D()) as Laya.Scene3D;
        scene.ambientColor = new Laya.Vector3(0.6, 0.6 ,0.6);

       
        var camera: Laya.Camera = (scene.addChild(new Laya.Camera(0, 0.1, 100))) as Laya.Camera;
        // camera.transform.translate(this._startPos);
        // camera.transform.lookAt(this._bornPos, this._up);
        camera.clearFlag = Laya.BaseCamera.CLEARFLAG_SOLIDCOLOR;
        camera.addComponent(CameraMoveScript);

        var directionLight: Laya.DirectionLight = scene.addChild(new Laya.DirectionLight()) as Laya.DirectionLight;
        directionLight.color = new Laya.Vector3(1, 1, 1);
        // directionLight.transform.worldMatrix.setForward(new Laya.Vector3(0, -1, 0));
        directionLight.transform.rotate(new Laya.Vector3( -3.14 / 3, 0, 0));

        var mapUrl = "res/LayaScene_CaiLiaoFuBen_YuanSu/Conventional/model.lh";
        var heroUrl = "res/LayaScene_CaiLiaoFuBen_YuanSu/Conventional/archer.lh";
        var effectUrl = "res/LayaScene_TestLayaScene/Conventional/eff_mage_2004@skill4abc.lh";

        var resource = [
			mapUrl, 
			heroUrl, 
			effectUrl
		];
		
		Laya.loader.create(resource, Laya.Handler.create(this, ()=>{
            //map
            let map = scene.addChild(Laya.loader.getRes(mapUrl)) as Laya.Sprite3D;

            //hero
            let hero = new Laya.Sprite3D('hero');
            hero.transform.translate(this._bornPos);
            scene.addChild(hero);

            let heroEntity = Laya.loader.getRes(heroUrl) as Laya.Sprite3D;
            heroEntity.transform.rotate(new Laya.Vector3(0,180,0),true, false);
            hero.addChild(heroEntity) as Laya.Sprite3D;

            let animCtl = hero.addComponent(EntityAnimCtl) as EntityAnimCtl;
            animCtl.registerMotion(Def.MotionType.idle, "standab");
            animCtl.registerMotion(Def.MotionType.run, "runab");
            animCtl.registerMotion(Def.MotionType.attacka, "attacka_1");

            let moveCtl = hero.addComponent(FirstPlayerMoveCtl) as FirstPlayerMoveCtl;
            moveCtl.camera = camera;
            moveCtl.cameraPosDelta = cameraDela;

            let heroCtl = hero.addComponent(FirstPlayerCtl) as FirstPlayerCtl;
            heroCtl.registEffectUrl(0, effectUrl);

            //effect
            heroCtl.spell(0);
        }));
    }

}